<?php
defined('_JEXEC') or die( 'Restricted access' );
class Fcore extends JTable
{
	public function __construct( $table, $key,  &$db )
	{
		parent::__construct( $table, $key, $db );
	}
	
	public function alist($order = '')
	{
		global $mainframe;
		
		$db = $this->_db;
		
		$limit		= $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart	= $mainframe->getUserStateFromRequest('limitstart', 'limitstart', 0, 'int');		
		$limitstart = ( $limit != 0 ? (floor($limitstart / $limit) * $limit) : 0 );
		
		$order = " ORDER BY " . ($order?$order:'id') . " DESC ";
		
		$query = 'SELECT COUNT(*)' .' FROM ' . $this->_tbl;
		$db->setQuery($query);
		$total = $db->loadResult();

		// Create the pagination object
		jimport('joomla.html.pagination');
		$pagination = new JPagination($total, $limitstart, $limit);
		
		$query = 'SELECT * FROM ' . $this->_tbl . $order;
		$db->setQuery($query, $pagination->limitstart, $pagination->limit);
		$rows = $db->loadObjectList();
		
		return array($pagination, $rows);
	}
	
	public function getLastInsertID()
	{
		$sql = "SELECT LAST_INSERT_ID()";
		
		$rows = $this->getBySQL($sql);
		
		if (!$rows || !is_array($rows)) return null;
		
		$row = array_shift($rows); 
		
		if (!$row || !is_array($row)) return null;		
		
		return array_shift($row);
	}
	
	/**
	 * get date by sql
	 * @param string $sql
	 * @param string $key the filed name of table, generally, primary key
	 * @return array 
	 */
	public function getBySQL($sql, $key = null)
	{
		$this->_db->setQuery($sql);
		return $this->_db->loadAssocList($key);
	}
	
	/**
	 * excute a query
	 * @param string $sql
	 * @return boolean
	 */
	public function excuteQuery($sql)
	{
		$this->_db->setQuery($sql);
		return (boolean)$this->_db->query();
	}
	
	/**
	 * 
	 * @param string $where
	 * @param string $field
	 * @return array
	 */
	public function getAll($where='', $field = '')
	{
		$sql = "SELECT " . ($field?$field:'*') . " FROM " . $this->_tbl .  " $where";
		return $this->getBySQL($sql);
	}
	
	/**
	 * get a row
	 * @param int $keyId value of primary key
	 * @param string $field 
	 * @return array 
	 */
	public function getRow($keyId, $field = '*')
	{
		$sql = "SELECT $field FROM " . $this->_tbl . " 
			WHERE " . $this->_tbl_key . "=" . intval($keyId) . " LIMIT 1";
		
		$rows = $this->getBySQL($sql);
		
		if (!$rows || !is_array($rows)) return null;
		
		return array_shift($rows);
	}
	
	/**
	 * get one filed value of one row
	 * @param int $keyId value of primary key
	 * @param string $field 
	 * @return string 
	 */
	public function getOne($keyId, $field = '*')
	{
		$row = $this->getRow($keyId, $field);
		
		if (!$row || !is_array($row)) return null;
		
		return array_shift($row);
	}
	
	/**
	 * 二维数组一维化
	 * @param array $arr
	 * @param string $key
	 * @param string $val
	 * @param string $default 
	 * @return array
	 */
	public function getOpt($arr, $key, $val, $default='')
	{
		$return = array();
		if ($default) $return[] = $default;
		
		foreach ((array) $arr as $v) {
			$return[$v[$key]] = $v[$val];
		}
		return $return;
	}
	
	/**
	 * 获取用户名
	 * @param int $uid 
	 */
	protected function getUserName($uid)
	{
		$db	= JFactory::getDBO();
		$query = 'SELECT name FROM #__users WHERE id=' . intval($uid);
		$db->setQuery($query);
		return array_pop(array_pop($db->loadAssocList()));
	}
}
